package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessEvaluate;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TrainEvaluateDetailsNewVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/5/10 10:06
 * 修改备注：
 * ================================================
 */
public class TrainEvaluateNewLocalDataActivity extends BaseActivity {

    @Bind(R.id.train_evaluate_tv_option_1_1)
    TextView trainEvaluateTvOption11;
    @Bind(R.id.train_evaluate_tv_option_1_2)
    TextView trainEvaluateTvOption12;
    @Bind(R.id.train_evaluate_tv_option_1_3)
    TextView trainEvaluateTvOption13;
    @Bind(R.id.train_evaluate_tv_option_1_4)
    TextView trainEvaluateTvOption14;
    @Bind(R.id.train_evaluate_tv_option_2_1)
    TextView trainEvaluateTvOption21;
    @Bind(R.id.train_evaluate_tv_option_2_2)
    TextView trainEvaluateTvOption22;
    @Bind(R.id.train_evaluate_tv_option_2_3)
    TextView trainEvaluateTvOption23;
    @Bind(R.id.train_evaluate_tv_option_2_4)
    TextView trainEvaluateTvOption24;
    @Bind(R.id.train_evaluate_tv_option_3_1)
    TextView trainEvaluateTvOption31;
    @Bind(R.id.train_evaluate_tv_option_3_2)
    TextView trainEvaluateTvOption32;
    @Bind(R.id.train_evaluate_tv_option_3_3)
    TextView trainEvaluateTvOption33;
    @Bind(R.id.train_evaluate_tv_option_3_4)
    TextView trainEvaluateTvOption34;
    @Bind(R.id.train_evaluate_tv_option_4_1)
    TextView trainEvaluateTvOption41;
    @Bind(R.id.train_evaluate_tv_option_4_2)
    TextView trainEvaluateTvOption42;
    @Bind(R.id.train_evaluate_tv_option_4_3)
    TextView trainEvaluateTvOption43;
    @Bind(R.id.train_evaluate_tv_option_4_4)
    TextView trainEvaluateTvOption44;
    @Bind(R.id.train_evaluate_tv_option_5_1)
    TextView trainEvaluateTvOption51;
    @Bind(R.id.train_evaluate_tv_option_5_2)
    TextView trainEvaluateTvOption52;
    @Bind(R.id.train_evaluate_tv_option_5_3)
    TextView trainEvaluateTvOption53;
    @Bind(R.id.train_evaluate_tv_option_5_4)
    TextView trainEvaluateTvOption54;
    @Bind(R.id.train_evaluate_tv_option_6_1)
    TextView trainEvaluateTvOption61;
    @Bind(R.id.train_evaluate_tv_option_6_2)
    TextView trainEvaluateTvOption62;
    @Bind(R.id.train_evaluate_tv_option_6_3)
    TextView trainEvaluateTvOption63;
    @Bind(R.id.train_evaluate_tv_option_6_4)
    TextView trainEvaluateTvOption64;
    @Bind(R.id.train_evaluate_edt_suggestion)
    EditText trainEvaluateEdtSuggestion;
    @Bind(R.id.view_train_evaluate_btn_submit)
    Button viewTrainEvaluateBtnSubmit;

    private String trainId; // 培训ID
    private TextView option[][];
    private String codes[][];
    private String code[] = {"", "", "", "", "", ""};
    private String titles[] = {"课程安排", "教学方法", "教学手段", "表达技巧", "教学互动", "组织工作"};

    private boolean isEdit = true; // 评价是否可以被编辑

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_train_evaluate_new_local_data);
        setTitle("培训评价");
    }

    @Override
    public void initMember() {

        option = new TextView[][]{
                {trainEvaluateTvOption11, trainEvaluateTvOption12, trainEvaluateTvOption13, trainEvaluateTvOption14},
                {trainEvaluateTvOption21, trainEvaluateTvOption22, trainEvaluateTvOption23, trainEvaluateTvOption24},
                {trainEvaluateTvOption31, trainEvaluateTvOption32, trainEvaluateTvOption33, trainEvaluateTvOption34},
                {trainEvaluateTvOption41, trainEvaluateTvOption42, trainEvaluateTvOption43, trainEvaluateTvOption44},
                {trainEvaluateTvOption51, trainEvaluateTvOption52, trainEvaluateTvOption53, trainEvaluateTvOption54},
                {trainEvaluateTvOption61, trainEvaluateTvOption62, trainEvaluateTvOption63, trainEvaluateTvOption64}};

        codes = new String[][]{
                {"1701", "1702", "1703", "1704"},
                {"1801", "1802", "1803", "1804"},
                {"1901", "1902", "1903", "1904"},
                {"2001", "2002", "2003", "2004"},
                {"2101", "2102", "2103", "2104"},
                {"2201", "2202", "2203", "2204"}};


        if (getIntent().hasExtra("JPush_evaluate")) {
            trainId = (String) getIntent().getSerializableExtra("JPush_evaluate");
        } else {
            trainId = (String) getIntent().getSerializableExtra("evaluateDetail");
            BusinessEvaluate.getEvaluateDetailsNew(this, trainId, mHandler);
            // TODO 展示已经评价的内容 调用评价接口或就收传递过来的参数
            isEdit = false;
            viewTrainEvaluateBtnSubmit.setVisibility(View.GONE);
            trainEvaluateEdtSuggestion.setEnabled(false);
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_EVALUATE_FOR_TRAIN_SUCCESS:
                        showToast("评价成功");
                        Intent intent = new Intent();
                        intent.putExtra("trainingId", trainId);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case MSG.MSG_EVALUATE_FOR_TRAIN_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_ALL_EVALUATE_LIST_SUCCESS:
                        setView(msg.obj.toString());
                        break;
                    case MSG.MSG_ALL_EVALUATE_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    /**
     * 设置评价详情
     *
     * @param json
     */
    private void setView(String json) {
        TrainEvaluateDetailsNewVO trainEvaluateDetailsNewVO = FastJsonUtil.getBean(json, "train", TrainEvaluateDetailsNewVO.class);

        if (trainEvaluateDetailsNewVO == null)
            return;

        code[0] = trainEvaluateDetailsNewVO.getCourseArrange().getCode();
        code[1] = trainEvaluateDetailsNewVO.getTeachMethod().getCode();
        code[2] = trainEvaluateDetailsNewVO.getTeachMeans().getCode();
        code[3] = trainEvaluateDetailsNewVO.getExpressionSkill().getCode();
        code[4] = trainEvaluateDetailsNewVO.getTeachInteraction().getCode();
        code[5] = trainEvaluateDetailsNewVO.getOrganizeWork().getCode();

        trainEvaluateEdtSuggestion.setText(trainEvaluateDetailsNewVO.getSuggestion());

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 4; j++) {
                if (code[i].equals(codes[i][j]))
                    setTextColor(i, j);
            }
        }
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.train_evaluate_tv_option_1_1, R.id.train_evaluate_tv_option_1_2, R.id.train_evaluate_tv_option_1_3, R.id.train_evaluate_tv_option_1_4, R.id.train_evaluate_tv_option_2_1, R.id.train_evaluate_tv_option_2_2, R.id.train_evaluate_tv_option_2_3, R.id.train_evaluate_tv_option_2_4, R.id.train_evaluate_tv_option_3_1, R.id.train_evaluate_tv_option_3_2, R.id.train_evaluate_tv_option_3_3, R.id.train_evaluate_tv_option_3_4, R.id.train_evaluate_tv_option_4_1, R.id.train_evaluate_tv_option_4_2, R.id.train_evaluate_tv_option_4_3, R.id.train_evaluate_tv_option_4_4, R.id.train_evaluate_tv_option_5_1, R.id.train_evaluate_tv_option_5_2, R.id.train_evaluate_tv_option_5_3, R.id.train_evaluate_tv_option_5_4, R.id.train_evaluate_tv_option_6_1, R.id.train_evaluate_tv_option_6_2, R.id.train_evaluate_tv_option_6_3, R.id.train_evaluate_tv_option_6_4, R.id.view_train_evaluate_btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.train_evaluate_tv_option_1_1:
                if (isEdit)
                    setTextColor(0, 0);
                break;
            case R.id.train_evaluate_tv_option_1_2:
                if (isEdit)
                    setTextColor(0, 1);
                break;
            case R.id.train_evaluate_tv_option_1_3:
                if (isEdit)
                    setTextColor(0, 2);
                break;
            case R.id.train_evaluate_tv_option_1_4:
                if (isEdit)
                    setTextColor(0, 3);
                break;
            case R.id.train_evaluate_tv_option_2_1:
                if (isEdit)
                    setTextColor(1, 0);
                break;
            case R.id.train_evaluate_tv_option_2_2:
                if (isEdit)
                    setTextColor(1, 1);
                break;
            case R.id.train_evaluate_tv_option_2_3:
                if (isEdit)
                    setTextColor(1, 2);
                break;
            case R.id.train_evaluate_tv_option_2_4:
                if (isEdit)
                    setTextColor(1, 3);
                break;
            case R.id.train_evaluate_tv_option_3_1:
                if (isEdit)
                    setTextColor(2, 0);
                break;
            case R.id.train_evaluate_tv_option_3_2:
                if (isEdit)
                    setTextColor(2, 1);
                break;
            case R.id.train_evaluate_tv_option_3_3:
                if (isEdit)
                    setTextColor(2, 2);
                break;
            case R.id.train_evaluate_tv_option_3_4:
                if (isEdit)
                    setTextColor(2, 3);
                break;
            case R.id.train_evaluate_tv_option_4_1:
                if (isEdit)
                    setTextColor(3, 0);
                break;
            case R.id.train_evaluate_tv_option_4_2:
                if (isEdit)
                    setTextColor(3, 1);
                break;
            case R.id.train_evaluate_tv_option_4_3:
                if (isEdit)
                    setTextColor(3, 2);
                break;
            case R.id.train_evaluate_tv_option_4_4:
                if (isEdit)
                    setTextColor(3, 3);
                break;
            case R.id.train_evaluate_tv_option_5_1:
                if (isEdit)
                    setTextColor(4, 0);
                break;
            case R.id.train_evaluate_tv_option_5_2:
                if (isEdit)
                    setTextColor(4, 1);
                break;
            case R.id.train_evaluate_tv_option_5_3:
                if (isEdit)
                    setTextColor(4, 2);
                break;
            case R.id.train_evaluate_tv_option_5_4:
                if (isEdit)
                    setTextColor(4, 3);
                break;
            case R.id.train_evaluate_tv_option_6_1:
                if (isEdit)
                    setTextColor(5, 0);
                break;
            case R.id.train_evaluate_tv_option_6_2:
                if (isEdit)
                    setTextColor(5, 1);
                break;
            case R.id.train_evaluate_tv_option_6_3:
                if (isEdit)
                    setTextColor(5, 2);
                break;
            case R.id.train_evaluate_tv_option_6_4:
                if (isEdit)
                    setTextColor(5, 3);
                break;
            case R.id.view_train_evaluate_btn_submit:
                for (int i = 0; i < 6; i++) {
                    if (StringUtil.isStrEmpty(code[i])) {
                        showToast("请对“" + titles[i] + "”做出选择");
                        return;
                    }
                }
                BusinessEvaluate.evaluateForTrainNew(TrainEvaluateNewLocalDataActivity.this, trainId, code, trainEvaluateEdtSuggestion.getText().toString(), mHandler);
                break;
        }
    }

    /**
     * 设置选中后的颜色
     *
     * @param j 大组下标 size = 6
     * @param k 小组下标 size = 4
     */
    private void setTextColor(int j, int k) {

        for (int i = 0; i < 4; i++) {
            option[j][i].setTextColor(getResources().getColor(R.color.colorGray));
            option[j][i].setBackgroundResource(R.drawable.shape_gray_radius_2dp);
        }

        option[j][k].setTextColor(getResources().getColor(R.color.app_color));
        option[j][k].setBackgroundResource(R.drawable.shape_app_color_width_2dp_radius_2dp);

        code[j] = codes[j][k];

    }
}
